package ink.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import ink.xiaobaibai.annotation.InsidePublicResources;
import ink.xiaobaibai.bo.UserSelectBo;
import ink.xiaobaibai.common.MyRole;
import ink.xiaobaibai.common.PageInfo;
import ink.xiaobaibai.entity.AdminUser;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.IAdminRoleService;
import ink.xiaobaibai.service.IAdminUserRoleService;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.service.IDepartmentService;
import ink.xiaobaibai.units.MySecurity;
import ink.xiaobaibai.vo.UserByAdminVo;
import ink.xiaobaibai.vo.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @description: 用户信息控制层(管理员专属)
 * @author: 小白白
 * @create: 2021-05-16
 **/

@RestController
@RequestMapping("/security/user")
@Slf4j
@CrossOrigin
@Api(tags = "后台用户接口")
public class SecurityUserController {

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private IAdminUserRoleService adminUserRoleService;

    @Autowired
    private IAdminRoleService adminRoleService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/userInfo")
    @ApiOperation("获取当前用户信息")
    @InsidePublicResources
    public ResponseFormat getUserInfo() {
        Integer userId = MySecurity.getUserId();
        String userNumber = MySecurity.getUserNumber();
        Set<String> roles = MySecurity.getRoles();
        Integer depId = this.adminUserService.getDepId(userId);
        return ResponseFormat.success(new UserInfo(userId, userNumber, roles, depId));
    }

    /**
     * 查看所有的用户信息(非使用者)
     */
    @PostMapping("/list")
    @ApiOperation("查看所用用户信息")
    @InsidePublicResources
    public ResponseFormat getUserList(@RequestBody UserSelectBo userSelectBo) {
        if (!MySecurity.isPower(MyRole.POWER_USER)) {
            String role = MySecurity.getRole();
            if (role.equals(MyRole.DISTRIBUTOR)) {
                //业务员查看不了
                return ResponseFormat.fail("无权限查看");
            } else if (role.equals(MyRole.AGENT)) {
                //经理只能查看自己部门的人员信息
                userSelectBo.setDepartmentId(this.adminUserService.getDepId(MySecurity.getUserId()));
            }
        }
        int count = this.adminUserService.getUserVoByAdminCount(userSelectBo);
        if (count != 0) {
            List<UserByAdminVo> list = this.adminUserService.getUserVoByAdminList(userSelectBo);
            return ResponseFormat.success(new PageInfo<>(list, count, userSelectBo.getPageSize()));
        }
        return ResponseFormat.success(new PageInfo<>(Collections.emptyList(), 0, userSelectBo.getPageSize()));
    }

    /**
     * 添加用户,并且赋予当前用户什么角色 基础角色(部长或者业务员)
     * 转
     */


    /**
     * 删除用户 [转移置admin模块,因为需要校验其机器]
     */

    /**
     * 修改密码
     */
    @PutMapping("/update/password")
    @InsidePublicResources
    public ResponseFormat updatePassword(@RequestBody @NotNull @Length(min = 8, max = 20) String password) {
        String userNumber = MySecurity.getUserNumber();
        UpdateWrapper<AdminUser> u1 = new UpdateWrapper<>();
        u1.eq("user_number", userNumber);
        u1.set("user_password", this.passwordEncoder.encode(password));
        this.adminUserService.update(u1);
        return ResponseFormat.success();
    }


}
